import {Cask} from 'cascadium';
import Voucher from '../';
import Accrual from '../../Accrual';

function assignDest(morn, dusk){
  morn.dest = dusk.curr;
  dusk.dest = morn.curr;
}

const decompose = (origEntries, {deductFunc}={}) => {

  const deducted = new Cask();
  const remained = new Cask();
  
  for (let entry of origEntries) {
    deductFunc(entry) ? deducted.push(entry) : remained.push(entry);
  }

  const summed = Voucher.from(deducted);

  const decomped = new Cask();
  for (let entry of remained) {
    const compensated = summed.copy();
    compensated.accrual = entry.accrual.oppo();
    decomped.push(compensated);
  }

  const result = new Cask();
  for (let i = 0 ; i < remained.length; i++) {
    assignDest(decomped[i], remained[i]);
    result.push(decomped[i], remained[i]);
  }

  // console.log('SINGEL', result.map(({accrual}={}) => accrual));

  return result;
}

export default decompose;